package com.qcby.types.exception;


import com.qcby.types.util.MessageUtils;

public class BusinessException extends RuntimeException{

    private ExceptionResponse response;

    public BusinessException() {
        super();
    }

    /**
     * @param message the message
     */
    public BusinessException(String message) {
        super(MessageUtils.message(message));
    }

    /**
     * message 包含占位符，占位符 {} 按照顺序替换为参数
     *
     * @param message
     * @param params
     */
    public BusinessException(String message, Object... params) {
        super(MessageUtils.message(message, params));
    }

    /**
     * 参数为异常枚举
     *
     * @param response
     */
    public BusinessException(IResponse response) {
        super(MessageUtils.getMessageFromResponse(response));
        this.response = new ExceptionResponse(response.getCode(), getMessage());
    }

    /**
     * message 包含占位符，占位符 {} 按照顺序替换为参数
     *
     * @param params the message
     */
    public BusinessException(IResponse response, Object... params) {
        super(MessageUtils.getMessageFromResponse(response, params));
        this.response = new ExceptionResponse(response.getCode(), getMessage());
    }

    /**
     * @param message the message
     * @param cause   the cause
     */
    public BusinessException(String message, Throwable cause) {
        super(MessageUtils.message(message), cause);
    }

    public BusinessException(String message, Throwable cause, Object... params) {
        super(MessageUtils.message(message, params), cause);
    }

    /**
     * @param cause the cause
     */
    public BusinessException(Throwable cause) {
        super(cause);
    }

    public IResponse getResponse() {
        return response;
    }

    public static class ExceptionResponse implements IResponse {

        private String code;
        private String message;

        public ExceptionResponse(String code, String message) {
            this.code = code;
            this.message = message;
        }

        @Override
        public String getCode() {
            return this.code;
        }

        @Override
        public String getMessage() {
            return this.message;
        }

    }
}
